package com.ruoyi.webConfig.controller;

import com.ruoyi.web.model.AppResult;
import com.ruoyi.webConfig.model.SysWebConfig;
import com.ruoyi.webConfig.service.WebConfigServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@Slf4j
public class WebConfigController {
    @Autowired
    private WebConfigServiceImpl webConfigService;

    @GetMapping("/sys/web/config")
    public AppResult<SysWebConfig> getWebConfig() {
        SysWebConfig webConfig = webConfigService.getWebConfig();
        log.info("站点信息：{}",webConfig.toString());
        return AppResult.success(webConfig);
    }

    @RequestMapping("/sys/web/update")
    public AppResult<Void> updateWebConfig(@RequestBody SysWebConfig sysWebConfig) {
        webConfigService.updateWebConfig(sysWebConfig);
        return AppResult.success();
    }

    @RequestMapping("sys/web/updateLogo")
    public AppResult<Void> updateLogo(@RequestBody String logo) {
//        log.info("logo:{}",logo);
        webConfigService.updateLogo(logo);
        return AppResult.success();
    }
    @RequestMapping("/sys/web/updateAuthorAvatar")
    public AppResult<Void> updateAuthorAvatar(@RequestBody String logo) {
        webConfigService.updateAuthorAvatar(logo);
        return AppResult.success();
    }

    @RequestMapping("/sys/web/updateCommentSwitchApi")
    public AppResult<Void> updateCommentSwitch(@RequestBody Map<String, Object> params) {
        log.info("params:{}", params);
        Integer openComment = null;
        // 处理可能的不同数据类型
        Object openCommentObj = params.get("openComment");
        if (openCommentObj instanceof Integer) {
            openComment = (Integer) openCommentObj;
        } else if (openCommentObj instanceof String) {
            try {
                openComment = Integer.parseInt((String) openCommentObj);
            } catch (NumberFormatException e) {
                return AppResult.failed("参数格式错误");
            }
        }

        webConfigService.updateCommentSwitch(openComment);
        return AppResult.success();
    }


    @RequestMapping("/sys/web/uploadTourAvatar")
    public AppResult<Void> updateTourAvatar(@RequestBody String tour) {
        webConfigService.updateTourAvatar(tour);
        return AppResult.success();
    }

}
